using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace LWSkyDriveAttachment
{
    public partial class LSDBrowser : Form
    {
        public LSDBrowser()
        {
            InitializeComponent();
            pnlPreview.Height = 0;
            webBrowser1.DocumentCompleted+=new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
            pnlPreview.LostFocus += new EventHandler(pnlPreview_LostFocus);
        }

        void pnlPreview_LostFocus(object sender, EventArgs e)
        {
            pnlPreview.Height = 0;
        }

        
        void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            
            //selectembed.aspx
            //self.aspx
            for (int i = 0; i < e.Url.Segments.Length; i++)
            {
                if (e.Url.Segments[i].Contains("selectembed") | e.Url.Segments[i].Contains("self"))
                {
                    WebBrowser b = sender as WebBrowser;
                    
                    HtmlElement elemEmbed = b.Document.GetElementById("embedProperty");
                    if (elemEmbed != null)
                        elemEmbed.AttachEventHandler("onselect", (EventHandler)delegate {
                            PopPutRequest(elemEmbed.GetAttribute("value"));
                        });
                    HtmlElement elemUrl = b.Document.GetElementById("urlProperty");
                    if (elemUrl != null)
                        elemUrl.AttachEventHandler("onselect",  (EventHandler)delegate {
                            PopPutRequest(elemUrl.GetAttribute("value"));
                        });
                    HtmlElement elemAr = b.Document.GetElementById("EmbedHtmlTextArea");
                    if (elemAr != null)
                        elemAr.AttachEventHandler("onselect",  (EventHandler)delegate {
                            PopPutRequest(elemAr.GetAttribute("value"));
                        });
                }
            }
            
        }

        void PopPutRequest(string text)
        {
            webBrowser2.DocumentText = text;
            
            pnlPreview.Height = 300;
        }

        private void btInsert_Click(object sender, EventArgs e)
        {
            m_out = webBrowser2.DocumentText;
            pnlPreview.Height = 0;
            this.Close();
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            pnlPreview.Height = 0;
        }

        private string m_out;

        public string FormattedOutput
        {
            get { return m_out; }
            set { m_out = value; }
        }
	
    }
}